/*
 *  Copyright 2004 Blandware (http://www.blandware.com)
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.blandware.atleap.webapp.taglib.core.grid;

import com.blandware.atleap.webapp.taglib.core.BaseHandlerTag;
import com.blandware.atleap.webapp.util.core.RequestUtil;
import org.apache.struts.taglib.TagUtils;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.PageContext;
import java.io.IOException;
import java.io.StringWriter;
import java.util.Map;


/**
 * <p>Generates a link for clearing a filter with given name or all filters on
 * a grid. Body of this tag will be rendered (as link text) if there is a filter
 * in current grid associated with field with specified name, or if there is at
 * least one filter if "all" attribute set to "true".
 * <br />
 * This tag is only valid when nested within <em>grid</em> tag.
 * </p>
 * <p>
 * Allowed attributes are:
 * <ul>
 * <li>
 * <b>fieldName</b> - name of field for which to remove filtering
 * </li>
 * <li>
 * <b>all</b> - if 'true', all filters for current grid will be cleared
 * </li>
 * </ul>
 * </p>
 * <p>
 * At least one of these attributes must be specified. <b>fieldName</b> has a
 * precedence (that is, if <code>fieldName="someField"</code> and
 * <code>all="true"</code>, only field with name 'someField' will be cleared
 * when user clicks generated link).
 * </p>
 * <p><a href="ClearFilterTag.java.html"><i>View Source</i></a></p>
 * <p/>
 *
 * @author Sergey Zubtsovskiy <a href="mailto:sergey.zubtsovskiy@blandware.com">&lt;sergey.zubtsovskiy@blandware.com&gt;</a>
 * @version $Revision: 1.10 $ $Date: 2006/08/03 10:08:17 $
 * @jsp.tag name="clearFilter"
 * body-content="scriptless"
 */
public class ClearFilterTag extends BaseHandlerTag {

	// ~ Instance variables

	/**
	 * Field name to remove filter from
	 */
	protected String fieldName = null;

	/**
	 * Indicates whether or not all filters must be removed from grid
	 */
	protected Boolean all = null;


	/**
     * Returns field name
     *
	 * @return field name
	 * @jsp.attribute required="false"
	 * rtexprvalue="true"
	 * type="java.lang.String"
	 * description="Field name to remove filter from"
	 */
	public String getFieldName() {
		return fieldName;
	}

    /**
     * Sets field name
     *
     * @param fieldName field name to set
     */
	public void setFieldName(String fieldName) {
		this.fieldName = fieldName;
	}

	/**
     * Returns whether or not all filters must be removed from grid
     *
	 * @return whether all filters must be removed from grid
	 * @jsp.attribute required="false"
	 * rtexprvalue="true"
	 * type="java.lang.Boolean"
	 * description="Indicates whether or not all filters must be removed from grid"
	 */
	public Boolean getAll() {
		return all;
	}

    /**
     * Sets whether or not all filters must be removed from grid
     *
     * @param all whether all filters must be removed from grid
     */
	public void setAll(Boolean all) {
		this.all = all;
	}

    /**
     * Processes the tag
     *
     * @throws JspException
     * @throws IOException
     */
	public void doTag() throws JspException, IOException {

		PageContext pageContext = (PageContext) getJspContext();

		GridTag parentGridTag = (GridTag) findAncestorWithClass(this, GridTag.class);

		if ( parentGridTag == null ) {
			JspTagException e = new JspTagException("Parent tag is invalid! This tag is only valid when nested within 'grid' tag");
			throw e;
		}

		if ( fieldName == null && all == null ) {
			throw new JspTagException("Either 'fieldName' or 'all' attributes must be specified!");
		}


		if ( fieldName != null || all.equals(Boolean.TRUE) ) {

			// Get request URL and parameter map from parent grid tag.
			// Put needed request parameters in this map and attach it to page URL

			String pageUrl = parentGridTag.getPageUrl();
			Map parameterMap = RequestUtil.prepareParameterMap(parentGridTag.getParameterMap());

			parameterMap.put("gridName", parentGridTag.getName());
			pageUrl += "?" + RequestUtil.createQueryStringFromMap(parameterMap, "&");

			if ( fieldName != null ) {
				parameterMap.put("clearFilter", fieldName);
			} else {
				parameterMap.put("clearAllFilters", "true");
			}

			StringWriter sw = new StringWriter();
			StringBuffer sb = sw.getBuffer();
			sb.append("<a href=\"").append(pageUrl).append("\"").append(prepareAttributes()).append(">");
			getJspBody().invoke(sw);
			sb.append("</a>");
			TagUtils.getInstance().write(pageContext, sw.toString());
		}

	}

}
